/** @odoo-module **/

import {ControlPanel} from "@web/search/control_panel/control_panel";
import { SearchBar } from "@web/search/search_bar/search_bar";
import { SearchModel } from "@web/search/search_model";

import {registry} from "@web/core/registry";
import {useService} from "@web/core/utils/hooks";
import { download } from "@web/core/network/download";
import {Component, onWillStart, onMounted, useChildSubEnv, useState, useRef} from "@odoo/owl";

export class SProductionWIPComponent extends Component {
    setup() {
        this.orm = useService("orm");

        this.state = useState({
            wipGroupData: [],
            pnlTotal: 0,
            setaTotal: 0,
            outboundTotal: 0,
            productType: 'all'
        });
        this.stationWIPLine = useState([]);

        this.stationId = null

        this.searchInput = useRef('input')
        onWillStart(async () => {
            // await this.stationWIP();
            // await this.computeMaterialConsumptionList();
        });
        onMounted(async () => {
            this.state.productType = this.props.action.context.product_type || 'all'
            await this.stationWIP();
            // await this.computeMaterialConsumptionList();
        })
    }

    async stationWIP() {
        const search = !!this.searchInput.el.value
        const data = await this.orm.call(
            "s.production.wip.two",
            "get_station_wip_data",
            [], {'domain': this.getDomain(), product_type: this.state.productType, search: search }
        );

        this.state.wipGroupData.splice(0, this.state.wipGroupData.length, ...data.wip_group_data);

        const { pnl_total, seta_total, outbound_total } = data;
        Object.assign(this.state, {
            pnlTotal: pnl_total,
            setaTotal: seta_total.toFixed(3),
            outboundTotal: outbound_total
        });
    }

    async clickStation(stationId) {
        this.stationId = stationId
        const data = await this.orm.call(
            "s.production.wip.two",
            "get_station_wip_line_data",
            [], {'station_id': stationId, 'domain': this.getDomain(), product_type: this.state.productType}
        )

        this.stationWIPLine.length = 0;
        this.stationWIPLine.unshift(...data)
    }

    getDomain() {
        if (this.searchInput.el.value){
            const searchInput = this.searchInput.el.value.trim()
            return ['|', ['product_id.name', 'ilike', searchInput], ['production_id.name', 'ilike', searchInput]]
        } else {
            return []
        }
    }

    async handleKeyDown(ev) {

         if (ev.key === "Enter") {
            ev.preventDefault();
            await this.stationWIP();
            await this.clickStation(this.stationId)
        }
    }
    async onDownload() {
        const url = `/download/sb/production/wip/report/excel/plan/${this.state.productType}`;
        const data = {};
        await download({ url , data });
    }

    async onDownloadProduction() {
        const url = `/download/sb/production/wip/report/excel/production/${this.state.productType}`;
        const data = {};
        await download({ url , data });
    }
}

SProductionWIPComponent.template = "sb_plan.SProductionWIPTwo";
SProductionWIPComponent.components = {
    ControlPanel,
    // SearchBar
};
registry.category("actions").add("s_production_wip_two", SProductionWIPComponent);